package com.dudu.controller;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.dudu.bean.CallStatusBean;
import com.dudu.bean.PushToCusRecordBean;
import com.dudu.bean.UserBean;
import com.dudu.service.ICallStatusService;
import com.dudu.service.IPushCallRecordService;
import com.dudu.service.impl.CallStatusServiceImpl;
import com.dudu.service.impl.PushCallRecordServiceimpl;

import core.CoreService;
/**
 * 能力监控未开发完
 * @author gaoxs
 *
 */
public class CallStatusController extends CoreService implements ICallStatusController {
	//当前登陆用户
	private  final String KEY_CURRENT_USER = "key_current_user";
	//通话状态
	private ICallStatusService callstatusServcie=new CallStatusServiceImpl();
	//话单处理
	private IPushCallRecordService iPushCallRecordService=new PushCallRecordServiceimpl();
	/**
	 * 通话状态信息
	 * @return
	 */
	public String queryAll(){
		log.info("CallStatusAction.queryAll");
		List<CallStatusBean> callList = null;
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 放在JSONObject里面的数据对象
		JSONArray jsonArray = new JSONArray();

		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		if (currentUser != null) {
			// 获取页面分页用信息
			String pageSql = this.getPageSql();
			// 获取查询参数信息
			String paramSql = this.convertParams();
			// 查询数据
			callList = callstatusServcie.queryAll(pageSql, paramSql);

			// 查询总数量
			int totalCall = callstatusServcie.getTotalCall(paramSql);
			// 将查询的数据转化为json格式
			jsonArray = JSONArray.fromObject(callList);

			JSONObject dataobject = new JSONObject();
			// 总数量信息
			dataobject.put("totalItemNum", totalCall);
			// 具体数据信息
			dataobject.put("items", jsonArray);
			object.put("data", dataobject);

			object.put("flag", "000000");
			object.put("desc", "success");
		} else {
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	/**
	 * 查询推送话单失败记录
	 * @return
	 */
	public String queryAllPushFailed(){
		log.info("PushCallRecordAction.queryAllPushFailed");
		List<PushToCusRecordBean> pushFailedList=null;
		//最后返回的对象
		JSONObject object = new JSONObject();
		//放在JSONObject里面的数据对象
        JSONArray jsonArray = new JSONArray();
        //获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			//获取页面分页用信息
	        String pageSql = this.getPageSql();
	        //获取查询参数信息
	        String paramSql = this.convertParams();
	        //查询数据
	        pushFailedList=iPushCallRecordService.queryPushFailed(pageSql,paramSql);
			//查询总数量
			int totalAcount=iPushCallRecordService.getTotalFailedPush( paramSql);
			//将查询的数据转化为json格式
			jsonArray = JSONArray.fromObject(pushFailedList);
			
			JSONObject dataobject = new JSONObject();
			//总数量信息
			dataobject.put("totalItemNum", totalAcount);
			//具体数据信息
	        dataobject.put("items", jsonArray);
	        object.put("data", dataobject);
			
			object.put("flag", "000000");
	        object.put("desc", "success");
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	/**
	 * 重推话单
	 * @return
	 */
	@Override
	public String repushCall() {
		log.info("PushCallRecordAction.repushCall");
		// 最后返回的对象
		JSONObject object = new JSONObject();
		// 获取当前登录用户信息
		UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		if (currentUser != null) {
			// 获取前台传入的data数据
			String dataStr = (String) this.getRequest().getParameter("data");
			// 转化为json对象
			JSONObject data = JSONObject.fromObject(dataStr);
			// 获取相应数据
			String id = data.getString("id");// id
			
			// 执行数据修改
			String ret = iPushCallRecordService.repushCall( id);
			
			//具体数据信息
			if(!ret.equals("-1")){
				object.put("result", ret);
				object.put("flag", "000000");
		        object.put("desc", "success");
			}else{
				object.put("flag", "100000");
			}
			
		}else{
			object.put("flag", "100000");
		}
		return returnData(object.toString());
	}
	
	
	/**
	 * 一键重推话单
	 * @return
	 */
	public String repushAllCall(){
		log.info("PushCallRecordAction.repushAllCall");
		//最后返回的对象
		JSONObject object = new JSONObject();
        //获取当前登录用户信息
        UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
		//判断如果用户信息不为空则继续下一步
		if (currentUser != null) {
			//获取查询参数信息
	        String paramSql = this.convertParams();
	        List<PushToCusRecordBean> pushToCusRecordList = iPushCallRecordService.queryPushFailed( "", paramSql);
	        String ret = "";
	        
	        // 如果meetImsUnusualList不为空，则继续
	        if(pushToCusRecordList != null && pushToCusRecordList.size() > 0){
	        	for(PushToCusRecordBean pushToCusRecord : pushToCusRecordList){
	        		// 执行数据修改
	    			ret += iPushCallRecordService.repushCall(pushToCusRecord.getId()+"");
	        	}
	        }
	        
	        // 具体数据信息
	        if(ret.indexOf("-1") == -1){
				object.put("flag", "000000");
				object.put("desc", "success");
	        }else{
	        	object.put("flag", "200000");
	        	object.put("desc", "partly failed");
	        }
	        
		}else{
        	object.put("flag", "100000");
        	object.put("desc", "user is null");
        }
		
		return returnData(object.toString());
	}
}
